package cn.script.opt;

import cn.script.domain.Point;
import cn.script.domain.RangePoint;
import cn.script.util.Delay;
import cn.script.util.LogOut;

/**
 * 窗口操作
 *
 * @author 周永波
 */
public class WinOpt {

    private long hwnd;

    private DmSoft dmSoft;

    public WinOpt(long hwnd) {
        this.hwnd = hwnd;
        this.dmSoft = new DmSoft();
    }

    public void bindWindow() {
        int ret = this.dmSoft.bindWindow(this.hwnd);
        if (ret != 1) {
            LogOut.out("窗口绑定失败");
            throw new RuntimeException("窗口绑定失败");
        }
        LogOut.out("窗口绑定成功");
    }

    public void unWindow() {
        this.dmSoft.unWindow();
    }

    public void moveTo(Point point) {
        this.dmSoft.moveTo(point.x(), point.y());
    }

    public void moveTo(Point point, int delay) {
        this.moveTo(point);
        Delay.delay(delay);
    }

    public void moveToEx(RangePoint rangePoint) {
        this.dmSoft.moveToEx(rangePoint.ps().x(), rangePoint.ps().y(),
                rangePoint.width(), rangePoint.high());
    }

    public void moveToEx(RangePoint rangePoint, int delay) {
        this.moveToEx(rangePoint);
        Delay.delay(delay);
    }

    public void leftClick(Point point) {
        this.moveTo(point);
        this.dmSoft.leftClick();
    }

    public void leftClick(Point point, int delay) {
        this.leftClick(point);
        Delay.delay(delay);
    }

    public void leftClick(int delay) {
        this.dmSoft.leftClick();
        Delay.delay(delay);
    }

    public void rightClick(int delay){
        this.dmSoft.rightClick();
        Delay.delay(delay);
    }

    public void rightClick(Point point) {
        this.moveTo(point);
        this.dmSoft.rightClick();
    }

    public void rightClick(Point point, int delay) {
        this.rightClick(point);
        Delay.delay(delay);
    }

    public void keypress(String key) {
        this.dmSoft.keyPress(key);
    }

    public void keypress(String key, int delay) {
        this.dmSoft.keyPress(key);
        Delay.delay(delay);
    }


    public String ocr(String color, RangePoint rp) {
        return this.dmSoft.ocr(rp.ps().x(), rp.ps().y(), rp.pe().x(), rp.pe().y(), color, 1.0);
    }


    public Point findStr(String text, String color) {
        return this.findStr(text, color, RangePoint.rp(0, 0, 800, 544));
    }


    public Point findStr(String text, String color, RangePoint rp) {
        String pointStr = this.dmSoft.findStrEx(rp.ps().x(), rp.ps().y(), rp.pe().x(), rp.pe().y(), text, color, 1.0);
        if (pointStr == null || pointStr.equals("")) {
            return null;
        }
        String[] split = pointStr.split(",");
        return Point.point(Integer.valueOf(split[1]), Integer.valueOf(split[2]));
    }


    public int leftClickStr(String text, String color, Point offset, RangePoint range, int delay) {
        int ret = this.leftClickStr(text, color, offset, range);
        Delay.delay(delay);
        return ret;
    }

    public int leftClickStr(String text, String color, Point offset, RangePoint range) {
        Point point = this.findStr(text, color, range);
        if (point == null) {
            return 0;
        }
        point.offset(offset);
        this.leftClick(point);
        return 1;
    }

    public int rightClickStr(String text, String color, Point offset, RangePoint range, int delay) {
        Point point = this.findStr(text, color, range);
        if (point == null) {
            return 0;
        }
        point.offset(offset);
        this.rightClick(point, delay);
        return 1;
    }

    public int rightClickStr(String text, String color, Point offset, RangePoint range) {
        Point point = this.findStr(text, color, range);
        if (point == null) {
            return 0;
        }
        point.offset(offset);
        this.rightClick(point);
        return 1;
    }
}
